
package com.scs.application.modules.cert.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.cert.entity.CertificateHospitalBusiness;
import com.scs.application.modules.cert.mapper.CertificateHospitalBusinessMapper;
import com.scs.application.modules.cert.request.CertificateHospitalBusinessQueryRequest;
import com.scs.application.modules.cert.service.CertificateHospitalBusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 院方业务资质定义 service 实现
 *
 */
@Slf4j
@Service
public class CertificateHospitalBusinessServiceImpl extends BaseServiceImpl<CertificateHospitalBusinessMapper, CertificateHospitalBusiness> implements CertificateHospitalBusinessService {

    @Override
    public List<CertificateHospitalBusiness> list(CertificateHospitalBusinessQueryRequest request) {
        if (StringUtils.isBlank(request.getHospitalId()) && UserUtils.isHospital()) {
            request.setHospitalId(UserUtils.getHospitalId());
        }
        List<CertificateHospitalBusiness> list = this.baseMapper.list(request);
        return list;
    }

    @Override
    public IPage<CertificateHospitalBusiness> page(CertificateHospitalBusinessQueryRequest request) {
        request.setHospitalId(UserUtils.getHospitalId());
        return this.baseMapper.page(request.getPage(), request);
    }

    @Override
    public boolean save(CertificateHospitalBusiness entity) {
        entity.setHospitalId(UserUtils.getHospitalId());
        List<CertificateHospitalBusiness> list = this.list(Wrappers.<CertificateHospitalBusiness>query()
                .eq("certificate_id", entity.getCertificateId())
                .eq("hospital_id", entity.getHospitalId()));
        if (list.size() != 0) {
            throw new BusinessException("资质不可重复定义");
        }
        return super.save(entity);
    }

}
